import copy
import os
import open3d as o3d


class PcdSample:
    def __init__(self, config_dict: dict):
        self.pcd_path = config_dict["pcd_path"]
        self.save_path = config_dict["save_path"]
        self.params = float(config_dict["params"])
        self.pcd = None
        self.pcd_data = None

    def load_pcd(self):
        """
        open3d加载点云
        :return:
        """
        self.pcd = o3d.io.read_point_cloud(self.pcd_path)

    def save_pcd(self, filtered_pcd, save_type=True):
        """
        open3d保存降采样后的点云
        :param filtered_pcd: 需要保存的pcd点云数据 (o3d.pointcloud)
        :param save_type: True: 保存为ascii， False: 保存为binary
        :return:
        """
        o3d.io.write_point_cloud(self.save_path, filtered_pcd, write_ascii=save_type)

    def sample_pcd(self):
        """
        点云体素降采样
        :return:
        """
        pcdd = copy.deepcopy(self.pcd)
        down_pcd = pcdd.voxel_down_sample(voxel_size=self.params)
        self.save_pcd(down_pcd, False)